package sandbox;

import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;

import static junit.framework.Assert.assertNotNull;

/**
 * Created by IntelliJ IDEA.
 * User: VMoskalenko
 * Date: 07.02.12
 * Time: 14:11
 */
public class LondonStepdefs {

    private WebDriver driver;
    private WebElement locationElement;

    public LondonStepdefs() {
        this.driver = new InternetExplorerDriver();
    }

    @Given("^I have accessed Hotwire's international home page$")
    public void accessHotwireInternationalHomePageStep() {
        driver.get("http://www.hotwire.com/uk");
    }

    @When("^I enter \"(.*)\" in FF$")
    public void enterLocationInFareFinderStep(String location) {
        locationElement = driver.findElement(By.name("location"));
        locationElement.clear();
        locationElement.sendKeys(location);
        driver.findElement(By.name("startDate")).clear();
        driver.findElement(By.name("startDate")).sendKeys("12/02/12");
        driver.findElement(By.name("endDate")).clear();
        driver.findElement(By.name("endDate")).sendKeys("14/02/12");
    }

    @When("^click the \"Search for your hotel\" button$")
    public void submitFareFinderStep() {
        locationElement.submit();
    }

    @Then("opaque result list is displayed")
    public void displayOpaqueResultListStep() throws InterruptedException {
        for (int i = 0; i < 15; i++) {
            try {
                driver.findElement(By.id("opaqueResultsList"));
                break;
            } catch (NoSuchElementException nsee) {
                Thread.sleep(1000);
            }
        }
        assertNotNull(driver.findElement(By.id("opaqueResultsList")));
    }
}